var ACTION = require("../../base/action");
var utils = require("../../utils/utils");
var time = require("../../utils/time");
var ocrGml = require("../../utils/ocrGml");

function getActionWalk(config, ad, contentType) {
  var actionWalk = new ACTION(contentType, config);
  actionWalk.goStart = function () {
    var ocrItem = ocrGml.captureExistOne("走路赚钱", true);
    if (ocrItem) {
      this.execFCount++;
      this.updateStorage();
      utils.clickOcrItem(ocrItem);
      sleep(utils.getRandom(2.5, 3, 1000));
    }
    return ocrItem;
  };

  actionWalk.beforeExec = function () {
    var UIText = utils.existsTextMatches(/领取\d+金币/);
    if (UIText) {
      this.execSCount++;
      this.updateStorage();
      utils.clickUIText(UIText);
      sleep(utils.getRandom(2, 3, 1000));
      return true;
    }
    var UIText = utils.existsOneTextStartsWith("还差");
    if (UIText) {
      this.execSCount++;
      this.updateStorage();
      this.afterExec();
      return false;
    }
    return false;
  };
  actionWalk.exec = function () {
    var contents = [
      //prettier-ignore
      "开心收下",
      "立即收下",
    ];
    var UIText = utils.existsOneTextStartsWith(contents);
    if (UIText) {
      utils.clickUIText(UIText);
      sleep(utils.getRandom(2, 3, 1000));
      return;
    }
    var contents = [
      //prettier-ignore
      "看视频再",
      "看广告再",
    ];
    var UIText = utils.existsOneTextStartsWith(contents);
    if (UIText) {
      utils.clickUIText(UIText);
      sleep(utils.getRandom(2, 3, 1000));
      var contents = ["开心收下", "立即收下"];
      var ocrItem = ocrGml.captureExistOne(contents, true);
      if (ocrItem) {
        utils.clickOcrItem(ocrItem);
        sleep(utils.getRandom(2, 3, 1000));
        return;
      }
      ad.init(1, function () {
        var ocrItem = ocrGml.captureExistOne([
          "走路奖励",
          "今日步数",
          "活动说明",
        ]);
        return ocrItem;
      });
      return;
    }
  };
  actionWalk.afterExec = function () {
    console.log(this.name, time.str(), "afterExec");
    back();
    sleep(utils.getRandom(2, 3, 1000));
  };
  return actionWalk;
}

module.exports = getActionWalk;
